/* Linus H, 3B, pjav1409 */
package pjav1409.oop.k2_13.upg3;


/**
 * Kort saves the data of a playcard. It can have the values of 1-13 where one is
 * Ace and 13 is Kings. It is one of the four suits: Hearts, Spades, Diamonds or
 * Clubs.
 * @author Linus
 */
public class Kort {
    private int type = 0;  //0 = Heart, 1 = spades, 2 = diamonds, 3 = clubs
    private int value = 0; //Values: 1 - 13

    /**
     * Default constructor, initiates the card to Five of Hearts.
     */
    public Kort(){
        //Sätt kortet till 5 of hearts genom att anropa en annan konstruktor
        this(1, 5);
    }
    /**
     * Constructor where the user can self initiate the card
     * @param type the suit of the card (as a text representation)
     * @param value the value of the card
     */
    public Kort(String type, int value){
        //Sätt de givna värderna
        this.setType(type);
        this.setValue(value);
    }
    /**
    * Constructor where the user can self initiate the card
     * @param type the suit of the card (as a numerical representation)
     * @param value the value of the card
     */
    public Kort(int type, int value){
        //Sätt de givna värderna
        this.setType(type);
        this.setValue(value);
    }

    /**
     * Set the suit of the card
     * @param type The name of the suit (Hearts/Spades/diamonds/clubs)
     */
    public void setType(String type){
        try {
            //Försök sätta färgen på kortet efter att ha converterat type-variablen till int
            this.setType(this.stringTypeToIntType(type));
        } catch (Exception ex) {
            //Be om ny input och återinitera objectet
        }
    }

    /**
    * Set the suit of the card.
     * @param type The number of the suit (0 = herts, 1 = spades, 2 = diamonds, 3 = clubs)
     */
    public void setType(int type){
        //Kontrollera att värdet är inom de givna gränserna
        if (type >= 0 && type <= 3){
            //Sätt inputvärdet till klassvariablen
            this.type = type;
        }
        else {
            //Kasta exception
        }
    }

    /**
     * Set the value of the card
     * @param value the value to be set (1-13)
     */
    public void setValue(int value){
        //Kontrollera att värdet är inom de givna gränserna
        if (value >= 1 && value <= 13){
            //Sätt inputvärdet till klassvariablen
            this.value = value;
        }
        else {
            //Kasta exception
        }
    }

    /**
     * convert the suit into a numerical representation
     * @param type name of the suit
     * @return int value of the suit; 0 = Heart, 1 = spades, 2 = diamonds, 3 = clubs
     */
    public int stringTypeToIntType(String type) throws Exception{
        //Testa inputsträngen mot de olika färgerna och retunera intvärde därefter
        if (type.equalsIgnoreCase("Heart")){
            return 0;
        }
        else if (type.equalsIgnoreCase("Spades")){
            return 1;
        }
        else if (type.equalsIgnoreCase("Diamonds")){
            return 2;
        }
        else if (type.equalsIgnoreCase("Clubs")){
            return 3;
        }
        else {
            //Kasta exception om ett felaktigt input är givet
            throw new Exception(type + " is not a valid card type!");
        }
    }

   /**
     * Converts the data in the variable <code>type</code> to a string representation
     * @return the suit of the card
     */
    public String intTypeToStringType(){
        //Kolla vilket intvärde typevariablen har och returnera färg därefter
        if (this.type == 0){
            return "Heart";
        }
        else if (this.type == 1){
            return "Spades";
        }
        else if (this.type == 2){
            return "Diamonds";
        }
        else if (this.type == 3){
            return "Clubs";
        }

        //Safe-sats, kommer aldrig nås
        return null;
    }

    /**
     * Converts the data in the variable <code>value</code> to a string representation;
     * 1 -> Ace, 11 -> Jack, 12-> Queen, 13 -> King.
     * @return the string that represent the value of the card
     */
    public String intValueToStringValue(){
        //kolla vilket värde value variablen har och konvertara de klädda korten
        //till deras "text"
        if(this.value == 1){
            return "Ace";
        }
        else if (this.value > 1 && this.value < 11){
            return Integer.toString(this.value);
        }
        else if (this.value == 11){
            return "Jack";
        }
        else if (this.value == 12){
            return "Queen";
        }
        else if (this.value == 13){
            return "King";
        }
        
        //Safe-sats, kommer aldrig nås
        return null;
    }

    /**
     * Returns the card in the format "Queen of Spades" or "4 of Hearts"
     * @return a text representation of the object
     */
    @Override
    public String toString(){
        //Returnera den formaterade strängen
        return this.intValueToStringValue() +  " of " + this.intTypeToStringType();
    }

    /**
     * Get the value of the card.
     * @return returns the value of the card as a string
     */
    public String getValue(){
        return this.intValueToStringValue();
    }

    /**
     * Get the suit (Hearts/Spades/Diamonds/Clubs) of the card.
     * @return returns the suit of the card as a string
     */
    public String getType(){
        return this.intTypeToStringType();
    }
}
